<template>
    <el-card class="box-card">
        <template #header>
            <div class="card-header">
                <span>登录</span>
            </div>
        </template>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">

            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" type="password" autocomplete="off" />
            </el-form-item>
           
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">Submit</el-button>
                <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
import { login } from '@/services/user'
const ruleFormRef = ref();
const router = useRouter();

const ruleForm = reactive({
    username: '',
    pwd: ''
})

const rules = reactive({

})

const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            // console.log('submit!')
            let res = await login(ruleForm.username,ruleForm.pwd);
            if(res.code == 1){
                ElMessage.success('登录成功');
                router.push('/student');
            }else{
                ElMessage.error('登录失败');
            }
        } else {
            ElMessage.error('验证失败');
            return false
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.box-card {
    width: 480px;
    margin: auto;
}
</style>